var postsData = require('../../../data/posts-data')
var app = getApp(); //拿到app.js中的全局变量
Page({
  data() {
    isPlayingMusic = false;
  },
  onLoad: function (option) {
    var postId = option.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      postData: postData
    });
    var postsCollected = wx.getStorageSync('posts_Collected');
    // 判断文章是否有被收藏的状态绑定到collected里
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      this.setData({
        collected: postCollected
      })
    }
    // 当postsCollected整个缓存体不存在时
    else {
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_Collected', postsCollected);
    }
    if (app.globaData.g_isPlayingMusic && app.globaData.g_currentMusicPostId===postId) {
      this.setData({
        isPlayingMusic : true,
      })
    }
    this.setMusicMonitor();
  },
  // 绑定系统音乐播放器和页面开始暂停同步
  setMusicMonitor: function () {
    var backgroundAudioManager = wx.getBackgroundAudioManager();
    var that = this;
    // 音乐开始
    backgroundAudioManager.onPlay(function () {
      that.setData({
        isPlayingMusic: true,
      })
      app.globaData.g_isPlayingMusic = true;
      app.globaData.g_currentMusicPostId = that.data.currentPostId;
    })
    // 音乐暂停
    backgroundAudioManager.onPause(function () {
      that.setData({
        isPlayingMusic: false,
      })
      app.globaData.g_isPlayingMusic = false;
      app.globaData.g_currentMusicPostId = null;
    })
    // 音乐停止
    backgroundAudioManager.stop()(function () {
      that.setData({
        isPlayingMusic: false,
      })
      app.globaData.g_isPlayingMusic = false;
      app.globaData.g_currentMusicPostId = null;
    })
  },
  // 当用户点击收藏按钮时
  onColletionTap: function (event) {
    this.getPostsCollectedSyc();
    // this.getPostsCollectedAsy();
  },
  // 收藏功能： 同步实现
  getPostsCollectedSyc: function (event) {
    var postsCollected = wx.getStorageSync('posts_Collected');
    // 获取当前被点击收藏的文章是否有被收藏
    var postCollected = postsCollected[this.data.currentPostId];
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    this.showToast(postsCollected, postCollected);
  },
  // 收藏功能： 异步实现
  getPostsCollectedAsy: function () {
    var that = this;
    wx.getStorage({
      key: "posts_Collected",
      success: function (res) {
        var postsCollected = res.data;
        // 获取当前被点击收藏的文章是否有被收藏
        var postCollected = postsCollected[that.data.currentPostId];
        postCollected = !postCollected;
        postsCollected[that.data.currentPostId] = postCollected;
        that.showToast(postsCollected, postCollected);
      }
    })
  },
  showModal: function (postsCollected, postCollected) {
    var that = this;
    wx.showModal({
      cancelColor: '#333',
      cancelText: '取消',
      confirmColor: '#666',
      confirmText: '确定',
      content: postCollected ? "收藏该文章?" : "取消收藏该文章",
      showCancel: true,
      title: '收藏',
      success: (res) => {
        if (res.confirm) {
          // 更新文章是否的缓存值
          wx.setStorageSync('posts_Collected', postsCollected);
          // 更新data的数据绑定 页面图片才会发生变化
          that.setData({
            collected: postCollected,
          });
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  showToast: function (postsCollected, postCollected) {
    // 更新文章是否的缓存值
    wx.setStorageSync('posts_Collected', postsCollected);
    // 更新data的数据绑定 页面图片才会发生变化
    this.setData({
      collected: postCollected,
    });
    // 显示消息提示框
    wx.showToast({
      title: postCollected ? "收藏成功" : "取消成功",
      duration: 1000
    })
  },
  onShareTap: function (event) {
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"
    ]
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "#405f80",
      success: function (res) {
        // res.cancel用户是否点击了取消
        // res.tapIndex 数组元素的符号，从0开始
        wx.showModal({
          title: '用户' + itemList[res.tapIndex],
          content: "现在还无法实现分享功能",
        })
      }
    })
  },
  onMusicTap: function (event) {
    var isPlayingMusic = this.data.isPlayingMusic; //判断是不是正在播放
    var backgroundAudioManager = wx.getBackgroundAudioManager();
    var currentPostId = this.data.currentPostId;
    var postData = postsData.postList[currentPostId];
    if (isPlayingMusic) {
      backgroundAudioManager.pause();
      this.setData({
        isPlayingMusic: false
      })
    } else {
      backgroundAudioManager.title = postData.music.title
      backgroundAudioManager.epname = '专辑名称'
      backgroundAudioManager.coverImgUrl = postData.music.coverImg
      backgroundAudioManager.src = postData.music.url // 设置了 src 之后会自动播放
      this.setData({
        isPlayingMusic: true
      })
    }

  }
});